package cn.data.collection.util;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import cn.data.collection.dto.CollectionListDTO;

public class DataUtils {
	public static final Map<String, CollectionListDTO> CASEDATA = new HashMap<String, CollectionListDTO>();// 所有的数据加载都在这里面
	public static final Map<String, Integer> MAXSHOULIHAODATA = new HashMap<String, Integer>();// 所有的数据加载都在这里面

	public synchronized static void data2File(Map<String, CollectionListDTO> collectData, String date)
			throws IOException {
		Map<String, CollectionListDTO> existData = read(date);
		existData.putAll(collectData);
		save(existData, date);
	}

	public static void data2FileMaxShoulihao(String fileName) throws IOException {
		Map<String, Integer> existData = readShoulihao(fileName);
		existData.putAll(DataUtils.MAXSHOULIHAODATA);
		saveMaxShoulihao(existData, fileName);
	}

	public static void save(Map<String, CollectionListDTO> data, String date) throws IOException {
		String fileName = "data\\" + (StringUtil.isRealEmpty(date) ? DateUtils.nowDate(null) : date) + ".dat";
		DataOutputStream out = null;
		try {
			out = new DataOutputStream(new FileOutputStream(fileName));
			Iterator<Map.Entry<String, CollectionListDTO>> iterator = data.entrySet().iterator();
			while (iterator.hasNext()) {
				CollectionListDTO objCollectionListDTO = iterator.next().getValue();
				out.writeUTF(objCollectionListDTO.getShoulihao());
				out.writeUTF(objCollectionListDTO.getCompanyName());
				out.writeUTF(objCollectionListDTO.getCollectionDateTime());
			}
		} catch (IOException e) {
			throw e;
		} finally {
			out.close();
			out = null;
		}
	}

	public static void saveMaxShoulihao(Map<String, Integer> data, String fileName) throws IOException {
		String fileLocation = "data\\" + (StringUtil.isRealEmpty(fileName) ? DateUtils.nowDate(null) : fileName)
				+ ".dat";
		DataOutputStream out = null;
		try {
			out = new DataOutputStream(new FileOutputStream(fileLocation));
			Iterator<Map.Entry<String, Integer>> iterator = data.entrySet().iterator();
			while (iterator.hasNext()) {
				Entry<String, Integer> entry = iterator.next();
				out.writeUTF(entry.getKey());
				out.writeUTF(entry.getValue().toString());
			}
		} catch (IOException e) {
			throw e;
		} finally {
			out.close();
			out = null;
		}
	}

	@SuppressWarnings("finally")
	public static Map<String, Integer> readShoulihao(String fileName) {
		Map<String, Integer> data = new HashMap<String, Integer>();
		DataInputStream dis = null;
		try {
			dis = new DataInputStream(new FileInputStream(new File("data\\"
					+ (StringUtil.isRealEmpty(fileName) ? DateUtils.nowDate(null) : fileName) + ".dat")));
			while (true) {
				data.put(dis.readUTF(), Integer.valueOf(dis.readUTF()));
			}
		} catch (Exception e) {
		} finally {
			try {
				if (dis != null) {
					dis.close();
				}
			} catch (IOException e) {
			}
			return data;
		}
	}

	@SuppressWarnings("finally")
	public static Map<String, CollectionListDTO> read(String date) {
		Map<String, CollectionListDTO> data = new HashMap<String, CollectionListDTO>();
		DataInputStream dis = null;
		try {
			dis = new DataInputStream(new FileInputStream(new File("data\\"
					+ (StringUtil.isRealEmpty(date) ? DateUtils.nowDate(null) : date) + ".dat")));
			while (true) {
				CollectionListDTO objCollectionListDTO = new CollectionListDTO();
				objCollectionListDTO.setShoulihao(dis.readUTF());
				objCollectionListDTO.setCompanyName(dis.readUTF());
				objCollectionListDTO.setCollectionDateTime(dis.readUTF());
				data.put(objCollectionListDTO.getShoulihao(), objCollectionListDTO);
			}
		} catch (Exception e) {
		} finally {
			try {
				if (dis != null) {
					dis.close();
				}
			} catch (IOException e) {
			}
			return data;
		}
	}

	public synchronized static void data2Cache(Map<String, CollectionListDTO> data) {
		CASEDATA.putAll(data);
	}

	public static void main(String[] args) {
		Map<String, CollectionListDTO> map = new HashMap<String, CollectionListDTO>();
		CollectionListDTO objCollectionListDTO = new CollectionListDTO();
		objCollectionListDTO.setShoulihao("setShoulihao");
		objCollectionListDTO.setCompanyName("setCompanyName");
		objCollectionListDTO.setCollectionDateTime("setCompanyName");
		map.put(objCollectionListDTO.getShoulihao(), objCollectionListDTO);

		CollectionListDTO objCollectionListDTO2 = new CollectionListDTO();
		objCollectionListDTO2.setShoulihao("setShoulihao2");
		objCollectionListDTO2.setCompanyName("setCompanyName2");
		objCollectionListDTO2.setCollectionDateTime("setCompanyName2");
		map.put(objCollectionListDTO2.getShoulihao(), objCollectionListDTO2);

		try {
			data2File(map, null);
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println(map);
	}
}
